package at.fhj.swd.pse.data;

import java.util.ArrayList;
import java.util.List;

import at.fhj.swd.pse.entities.Activity;
import at.fhj.swd.pse.entities.Community;
import at.fhj.swd.pse.entities.EActivityType;
import at.fhj.swd.pse.entities.User;

public class ActivityDAOMock implements ActivityDAO {

	private List<Community> m_communities = new ArrayList<Community>();
	private List<User> m_users = new ArrayList<User>();
	private List<Activity> m_activities = new ArrayList<Activity>();
	
	@Override
	public Activity insert(Activity entity) {
		m_activities.add(entity);
		return entity;
	}

	@Override
	public Activity get(int id) {
		for (Activity entity : m_activities) {
			if (entity.getId() == id){
				return entity;
			}
		}
		return null;
	}

	@Override
	public List<Activity> getAll() {
		return m_activities;
	}

	@Override
	public void remove(Activity entity) {
		for (int i = 0; i < m_activities.size(); i++) {
			if (entity.getId() == m_activities.get(i).getId()){
				m_activities.remove(i);
			}
		}
	}

	@Override
	public Activity update(Activity entity) {
		for (Activity activity : m_activities) {
			if (activity.getId() == entity.getId()){
				m_activities.remove(activity);
				m_activities.add(entity);
			}
		}
		return entity;
	}

	@Override
	public List<Activity> getGlobalActivities() {
		List<Activity> m_global = new ArrayList<Activity>();
		for (Activity activity : m_activities) {
			if (activity.getType() == EActivityType.Global){
				m_global.add(activity);
			}
		}
		return m_global;
	}

	@Override
	public List<Activity> getAllCommunityActivities(int userId) {
		List<Activity> m_community = new ArrayList<Activity>();
		for (Activity activity : m_activities) {
			if (activity.getType() == EActivityType.Community){
				m_community.add(activity);
			}
		}
		return m_community;
	}

	@Override
	public List<Activity> getFriendActivities(int userId) {
		List<Activity> m_friends = new ArrayList<Activity>();
		for (User user : m_users) {
			if (user.getId() == userId){
				for (User friend : user.getFriends()) {
					for (Activity activity : m_activities) {
						if (activity.getCreator() == friend){
							m_friends.add(activity);
						}
					}
				}
			}
		}
		return m_friends;
	}

	@Override
	public List<Activity> getOwnActivities(int userId) {
		List<Activity> m_own = new ArrayList<Activity>();
		for (Activity activity : m_activities) {
			if (activity.getType() == EActivityType.Friend){
				if (activity.getCreator().getId() == userId){
				m_own.add(activity);
				}
			}
		}
		return m_own;
	}

	@Override
	public void addToCommunity(int activityId, int communityId) {
		for (Activity activity : m_activities) {
			if (activity.getId() == activityId){
				for (Community community : m_communities) {
					if (community.getID() == communityId){
						community.addActivity(activity);
					}
				}
			}
		}


	}
	

	public void addUser(User user){
		m_users.add(user);
	}
	
	public void addCommunity(Community community){
		m_communities.add(community);
	}

}
